package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.interactive.UserInteractive;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.InteractiveModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_HATE_PLAYER_RESP, desc = "跨服蛊惑玩家响应")
public class CrossHateRespCmd extends AbstractCommand {
	PlayerProto.CrossHatePlayerRespMsg reqMsg;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = PlayerProto.CrossHatePlayerRespMsg.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		InteractiveModule interactiveModule = player.getModule(InteractiveModule.class);
		interactiveModule.removeOpHateUserId(reqMsg.getHateUserId());
		Property consume = new Property(GameConfig.GAME_ITEM_HATE, BigInteger.ONE);
		player.getModule(PreReduceModule.class).restorePreReduce(consume);
		if(reqMsg.getRet() == 0){
			player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.System, eLogMoneyType.HatePlayer);
			UserInteractive userInteractive = interactiveModule.getUserInteractive();
			if(!userInteractive.hasHateUserId(reqMsg.getHateUserId())) {
				userInteractive.addHateUserId(reqMsg.getHateUserId());
				interactiveModule.syncUserInteractive();
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		PlayerProto.HatePlayerRespMsg.Builder respMsg = PlayerProto.HatePlayerRespMsg.newBuilder();
		respMsg.setRet(reqMsg.getRet());
		if (0 == reqMsg.getRet()) {
			respMsg.setHeartCount(reqMsg.getHeartCount());
			respMsg.setEarnSpeed(reqMsg.getEarnSpeed());
			respMsg.setEffectValue(reqMsg.getEffectValue());
		}
		player.sendPacket(Protocol.U_HATE_PLAYER, respMsg);
	}
}
